/**
 * @ author: 汪孟苹
 */
define(["jquery","jquery.dataTables","bootstrap.dataTables",
	"comm/datatable/datatable.servers"],
    function($,$jtable,$btable,$servers){
	var datatable = {};

    datatable.table = "";

    /* 出错后的回调函数 */
    var errorCallback = function(obj,msg){
        var h = '<div class="datatable-error-html">' + msg + '</div>';
        obj.html(h);
    }

    /* ajax请求出错时的统一处理 */
    var error = function(obj,status,msg,callback) {
        switch (status) {
            case 0:
                callback(obj,"网络连接异常，请检查您的网络！");
                break;
            case 302:
                var config = $root.init();
                window.location.href = config.route.login;
                break;
            case 404:
                callback(obj,"您请求的服务不存在！");
                break;
            case 500:
                callback(obj,msg);
                break;
            default:
                break;
        }
    };

	datatable.init = function(params){
        $("#"+params.id).css({"width":"100%"});
        $(document).ready(function(){
    		var options = $servers.getOptions(params.options);
    		datatable.table = $("#"+params.id).dataTable(options);
            /* 出错时的处理 */
            $.fn.dataTable.ext.errMode = function( settings, tn, msg){};      // 覆盖掉警告弹框（如果有警告的话）
            datatable.table.on('xhr.dt',function (e, settings, json, xhr) {
                var obj = $(".content-main .panel-body");
                error(obj,xhr.status,xhr.msg,errorCallback);
            });
        });
	}

	return datatable;
});